package com.wondertek.poms.core.filter.impl;

import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.core.filter.BaseRuleFilter;
import com.wondertek.poms.core.filter.RuleFilterChain;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.IRulePo;
import com.wondertek.poms.dao.po.PcProductRule;
import com.wondertek.poms.dao.po.PrdInfoRule;


/**
 * <p><b>NeedDRMFilter Description:</b> 数字版权保护过滤</p>
 * @author douzi
 * <b>DATE</b> 2020年11月13日 下午6:19:46
 */
public class NeedDRMFilter extends BaseRuleFilter {

	@Override
	public void doFilter(IRulePo rule, Content c, RuleFilterChain chain) {
		if (rule instanceof PrdInfoRule) {
			PrdInfoRule r = (PrdInfoRule) rule;
			this.baseFilter(r.getPrdRuleId(),
					r.getNeedDRM(),
					c.getNeedDRM(),
					chain,
					"数字版权保护-NeedDRM");
		} else if (rule instanceof PcProductRule) {
			PcProductRule r = (PcProductRule) rule;
			this.baseFilter(r.getId(),
					r.getDigitalCopyrightProtection() != null ? r.getDigitalCopyrightProtection() : "", 
					c.getNeedDRM(),
					chain, 
					"数字版权保护-DigitalCopyrightProtection");
		}

		chain.doFilter(rule, c, chain);
	}
	
	/**
	 * <p>Title: baseFilter</p>
	 * <p>Description: 覆盖上级方法，实现自己复杂业务 </p>
	 * @param id
	 * @param ruleStr
	 * @param voStr
	 * @param chain
	 * @param comments
	 */
	protected void baseFilter(Long id, String ruleStr, String voStr, 
			RuleFilterChain chain, String comments) {
		if (chain.isFlag() && StringUtil.isNotBlank(ruleStr)) {
			String vo = StringUtil.trim(voStr);
			
			if(StringUtil.isNotBlank(voStr)) {//0-是， 1-否, null-否
				if("0".equals(StringUtil.null2Str(ruleStr))) {
					chain.setFlag(Boolean.FALSE);
				}
			}else {
				if(!ruleStr.equals(vo)) {
					chain.setFlag(Boolean.FALSE);
				}
			}
			
			log(id, chain, comments, ruleStr, vo);
		}
	}
}
